
const jwt = require('jsonwebtoken')

const { UserModel,BannerModel} = require('../db/model')

const { secret, expires } = require('../config')
const login = async (req, res) => {
  const { username, password } = req.body

  const result = await UserModel.findOne({ username, password })

  // new BannerModel({
  //   title:'003',
  //   img_url:'../public/banner/003.jpg'
  // }).save()

  if (!result) return res.send({ code: 0, massage: '登录失败' })

  const token = 'Bearer ' + jwt.sign({ id: result._id }, secret, { expiresIn: expires })
  res.send({
    code: 1,
    massage: '登录成功',
    token,
    uesr_id: result._id
  })
}

const info = async (req, res) => {
  const { id } = req.params

  // 根据 id 获取个人信息
  const result = await UserModel.findById(id)



  // 返回给前端
  res.send({
    code: 1,
    message: '获取个人信息成功',
    info: {
      id: result.id,
      nickname: result.nickname,
      username: result.username,
      age: result.age,
      gender: result.gender,
      avatar: result.avatar,
      desc: result.desc
    }
  })
}
// const info = async (req, res) => {
//     const { id } = req.params

//     const result = await UserModel.findById(id)
//     console.log(result);

//     res.send({
//         code: 1,
//         message: '获取个人信息成功',
//         info: {
//             id: result.id,
//             nickname: result.nickname,
//             username: result.username,
//             age: result.age,
//             gender: result.gender,
//             avatar: result.avatar,
//             desc: result.desc
//         }
//     })
// }

module.exports = {
  login,
  info
}   